import java.util.Arrays;

public class Sort {
    public static void selectSort(int[]array){
        for (int i = 0; i < array.length; i++) {
            for (int j = i+1; j < array.length; j++) {
                if(array[j]<array[i]){
                    int tmp=array[i];
                    array[i]=array[j];
                    array[j]=tmp;
                }
            }
        }
    }

    public static void heapSort(int[]array){
        createaHeap(array);
        int end=array.length-1;
        while(end>0){
            int tmp=array[0];
            array[0]=array[end];
            array[end]=tmp;
            shiftDown(array,0,end);
            end--;
        }
    }
    public static void createaHeap(int[]array){
        for (int parent = (array.length-1-1)/2; parent >=0 ; parent--) {
            shiftDown(array,parent,array.length);
        }
    }
    public static void shiftDown(int[]array,int parent,int len){
        int child=2*parent+1;
        while(child<len){
            if(child+1<len&&array[child+1]>array[child]){
                child++;
            }
            if(array[parent]<array[child]){
                int tmp=array[parent];
                array[parent]=array[child];
                array[child]=tmp;
                parent=child;
                child=2*parent+1;
            }else{
                break;
            }
        }
    }

    public static void bubbleSort(int[]array){
        for (int i = 0; i < array.length-1; i++) {
            boolean flg=false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j]>array[j+1]){
                    int tmp=array[j+1];
                    array[j+1]=array[j];
                    array[j]=tmp;
                    flg=true;
                }
            }
            if(flg==false){
                break;
            }
        }
    }


    /*public static void mergeSort(int []array){
        mergeInternal(array,0,array.length-1);
    }
    public static void mergeInternal(int[]array,int start,int end){
        if(start>=end){
            return;
        }
        int mid=start+((end-start)>>>1);
        mergeInternal(array,start,mid);
        mergeInternal(array,mid+1,end);
        merge(array,start,mid,end);
    }*/
    public static void merge(int[]array,int start,int mid,int end){
        int[]tmp=new int[end-start+1];
        int k=0;
        int s1=start;
        int e1=mid;
        int s2=mid+1;
        int e2=end;
        while(s1<=e1&&s2<=e2){
            if(array[s1]<array[s2]){
                tmp[k++]=array[s1++];
            }else{
                tmp[k++]=array[s2++];
            }
        }
        while(s1<=e1){
            tmp[k++]=array[s1++];
        }
        while(s2<=e2){
            tmp[k++]=array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[i+start]=tmp[i];
        }
    }
    public static void mergeSort(int[]array){
        int nums=1;
        while(nums<array.length){
            for (int i = 0; i < array.length; i+=nums*2) {
                int start=i;
                int mid=start+nums-1;
                if(mid>=array.length){
                    mid=array.length-1;
                }
                int right=mid+nums;
                if(right>=array.length){
                    right=array.length-1;
                }
                merge(array,start,mid,right);
            }
            nums*=2;
        }
    }



    public static void main(String[] args) {
        int[]array={55,44,66,33,23,12,7};
        mergeSort(array);
        //selectSort(array);
        //heapSort(array);
        //bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }

}
